package com.burgeon.backend.EnterpriseWeChat.rest.Brand;

import com.burgeon.backend.EnterpriseWeChat.entity.DimDef;
import com.burgeon.backend.EnterpriseWeChat.entity.Product;

import com.burgeon.backend.EnterpriseWeChat.rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 购物车推荐
 */
public class getFilterData extends RESTBase {
    private final static String RANGE = "20";//分页数量
    private String ip;
    private String account;
    private String password;

    public getFilterData(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public ArrayList<DimDef> getData(String dimFlag,int page) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(dimFlag,page), url);
//        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(new JSONArray(str).getJSONObject(0).getString("message"));
        }

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<DimDef> dimDefs= new ArrayList<>();
        if(jsonArray.length()>0){
            for (int i = 0; i <jsonArray.length(); i++) {
                DimDef dimDef = new DimDef();
                dimDef.setAttribName(jsonArray.getJSONArray(i).getString(0));
                dimDef.setDimDefId(jsonArray.getJSONArray(i).getString(1));
                dimDef.setDimDefName(jsonArray.getJSONArray(i).getString(2));
               dimDefs.add(dimDef);
//                System.err.println(dimDef);
            }
        }
        System.err.println(dimDefs.size());
        return dimDefs;
    }

    private MultiValueMap Data(String dimFlag,int page) {
        HashMap params = new HashMap();
        ArrayList colums = new ArrayList();//查询字段

        colums.add("ATTRIBNAME");//
        colums.add("'M_DIMDEF_ID'");//
        colums.add("'M_DIMDEF_ID;NAME'");//属性名称

        params.put("columns", colums);
        params.put("table", "M_DIM");//表名
        params.put("count", "true");


        if(page!=0){
            System.err.println("page"+page);
            params.put("range",RANGE);
            int r = Integer.valueOf(RANGE);
            params.put("start",String.valueOf((page-1)*r));
        }else {
            params.put("range",5000);
        }


        HashMap paramss = new HashMap();

        paramss.put("column","'DIMFLAG'");
        String a = "'="+dimFlag+"'";
        paramss.put("condition",a);

        HashMap orderby = new HashMap();

        orderby.put("column", "ATTRIBNAME");
        orderby.put("asc", "true");
        ArrayList list = new ArrayList();
        list.add(orderby);

        params.put("orderby", list);

        params.put("params", paramss);
//        System.err.println(paramss);
        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        getFilterData g = new getFilterData("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(g.getData("DIM2",1));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
